package org.wisteria.framework.util;

import java.util.ArrayList;
import java.util.List;

public class TreeUtils {

    /**
     * 两层循环实现建树
     *
     * @param treeNodes 传入的树节点列表
     * @return
     */
    public static <T extends TreeNode> List<T> bulid(List<T> treeNodes, Object root) {

        List<T> trees = new ArrayList<>();

        for (T treeNode : treeNodes) {

            if (treeNode.getParentId() == null || root.equals(treeNode.getParentId())) {
                trees.add(treeNode);
            }

            for (T it : treeNodes) {
                if (it.getParentId() != null && it.getParentId().equals(treeNode.getId())) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList<TreeNode>());
                    }
                    treeNode.add(it);
                }
            }
        }
        return trees;
    }

    public static <T extends TreeNode> List<T> bulids(List<T> treeNodes, List<String> rootList) {

        List<T> trees = new ArrayList<>();

        for (T treeNode : treeNodes) {

            if (treeNode.getParentId() == null || rootList.contains(treeNode.getParentId())) {
                trees.add(treeNode);
            }

            for (T it : treeNodes) {
                if (it.getParentId() != null && it.getParentId().equals(treeNode.getId())) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList<TreeNode>());
                    }
                    treeNode.add(it);
                }
            }
        }
        return trees;
    }

    public static <T extends TreeNode> List<T> bulid(List<T> treeNodes, Object root, int level) {
        List<T> trees = new ArrayList<>();

        for (T treeNode : treeNodes) {

            if (root.equals(treeNode.getParentId())) {
                trees.add(treeNode);
            }

            for (T it : treeNodes) {
                if (it.getParentId().equals(treeNode.getId())) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList<TreeNode>());
                    }
                    treeNode.add(it);
                }
            }
        }
        return trees;
    }

    /**
     * 使用递归方法建树
     *
     * @param treeNodes
     * @return
     */
//    public <T extends TreeNode> List<T> buildByRecursive(List<T> treeNodes, Object root) {
//        List<T> trees = new ArrayList<T>();
//        for (T treeNode : treeNodes) {
//            if (root.equals(treeNode.getParentId())) {
//                trees.add(findChildren(treeNode, treeNodes));
//            }
//        }
//        return trees;
//    }

    /**
     * 递归查找子节点
     *
     * @param treeNodes
     * @return
     */
//    public <T extends TreeNode> T findChildren(T treeNode, List<T> treeNodes) {
//        for (T it : treeNodes) {
//            if (treeNode.getId() == it.getParentId()) {
//                if (treeNode.getChildren() == null) {
//                    treeNode.setChildren(new ArrayList<>());
//                }
//                treeNode.add(findChildren(it, treeNodes));
//            }
//        }
//        return treeNode;
//    }
}

